//
//  HistoryViewController.m
//  project
//
//  Created by 圆圆陈 on 2018/3/1.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "HistoryViewController.h"

@interface HistoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@end

@implementation HistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *StateView = [[UIView alloc] init];
    StateView.frame = CGRectMake(0, 0, kSCREEN_WIDTH, 20);
    StateView.backgroundColor = BBIGSTATECOLOR;
    [self.view addSubview:StateView];
    
    UIView *naView = [[UIView alloc] init];
    naView.frame = CGRectMake(0, 20+FitSimpleIpx(0, 20), kSCREEN_WIDTH, 44);
    naView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:naView];
    
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20),screenWidth, 44)];
    lable.textColor=[UIColor blackColor];
    lable.font=SNFONT(17);
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = HHH_Text(@"历史日程");
    
    [self.view addSubview:lable];
    
    UIButton *leftButton = [[UIButton alloc] init];
    leftButton.frame = CGRectMake(5, 20+FitSimpleIpx(0, 20), 44, 44);
    [leftButton setImageNormal:@"进入"];
    [leftButton addTarget:self action:@selector(leftbuttonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20+FitSimpleIpx(0, 20)+44+10, HHHWIDTH, HHHHEIGHT-20-FitSimpleIpx(0, 20)-44-2) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
//    SNShadowView *shadowLine = [[SNShadowView alloc] init];
//    shadowLine.frame = SNFRAME(0,20+FitSimpleIpx(0, 20) + 44, HHHWIDTH, 0);
//    [shadowLine showWithType:1];
//    [self.view addSubview:shadowLine];
    UIImageView *shadowLine = [[UIImageView alloc] init];
    shadowLine.frame = SNFRAME(0, 20+FitSimpleIpx(0, 20) + 44 -2, HHHWIDTH, 2);
    shadowLine.image = [UIImage image:@"lineImage"];
//    [self.view addSubview:shadowLine];
    self.view.backgroundColor = UIColorHEX(0xf0f2f5);
}
- (void)leftbuttonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return SN_USER.historyArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    SNcalendarModel *model = SN_USER.historyArray[indexPath.row];
    cell.textLabel.text = model.info;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%ld-%02ld-%02ld %02ld:%02ld",model.year,model.month,model.day,model.hours,model.mins];
    cell.textLabel.font = [UIFont systemFontOfSize:FitFont(18)];
    
    UIView*view1 = [[UIView alloc] initWithFrame:SNFRAME(0, 70, screenWidth, 10)];
    view1.backgroundColor = UIColorHEX(0xf0f2f5);
    [cell addSubview:view1];
    
    UIView*view2 = [[UIView alloc] initWithFrame:SNFRAME(20, 5, screenWidth-20, 0.5)];
    view2.backgroundColor = UIColorHEX(0xd9d9d9);
    [view1 addSubview:view2];
    return cell;
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return   UITableViewCellEditingStyleDelete;
}
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:HHH_Text(@"删除")  handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        //删除当前的model
        [SN_USER.historyArray removeObjectAtIndex:indexPath.row];
        [SN_USER updateToDB];
        [tableView reloadData];
    }];
    
    rowAction.backgroundColor = SNCOLORBLACK;
    return @[rowAction];
}
- (nullable UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(tvos){
    
    // delete action
    UIContextualAction *deleteAction = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:HHH_Text(@"删除") handler:^(UIContextualAction * _Nonnull action,__kindof UIView * _Nonnull sourceView,
        void (^ _Nonnull completionHandler)(BOOL)){
            [tableView setEditing:NO animated:YES];  // 这句很重要，退出编辑模式，隐藏左滑菜单
            //删除当前的model
            [SN_USER.historyArray removeObjectAtIndex:indexPath.row];
            [SN_USER updateToDB];
            [tableView reloadData];
            completionHandler(true);
        }];
    deleteAction.backgroundColor = SNCOLORBLACK;
    UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];
    actions.performsFirstActionWithFullSwipe = NO;
    return actions;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
